home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / aminet / util / gnu / emacs_src_18_58.lha / emacs-18.58 / lisp / fill.el < prev    next >
Lisp/Scheme  |  1992-02-21  |  9KB  |  252 lines

  1. ;; Fill commands for Emacs
  2. ;; Copyright (C) 1985, 1986 Free Software Foundation, Inc.
  3.  
  4. ;; This file is part of GNU Emacs.
  5.  
  6. ;; GNU Emacs is free software; you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation; either version 1, or (at your option)
  9. ;; any later version.
  10.  
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. ;; GNU General Public License for more details.
  15.  
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  18. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20.  
  21. (defun set-fill-prefix ()
  22.   "Set the fill-prefix to the current line up to point.
  23. Filling expects lines to start with the fill prefix
  24. and reinserts the fill prefix in each resulting line."
  25.   (interactive)
  26.   (setq fill-prefix (buffer-substring
  27.              (save-excursion (beginning-of-line) (point))
  28.              (point)))
  29.   (if (equal fill-prefix "")
  30.       (setq fill-prefix nil))
  31.   (if fill-prefix
  32.       (message "fill-prefix: \"%s\"" fill-prefix)
  33.     (message "fill-prefix cancelled")))
  34.  
  35. (defun fill-region-as-paragraph (from to &optional justify-flag)
  36.   "Fill region as one paragraph: break lines to fit fill-column.
  37. Prefix arg means justify too.
  38. From program, pass args FROM, TO and JUSTIFY-FLAG."
  39.   (interactive "r\nP")
  40.   (save-restriction
  41.     (narrow-to-region from to)
  42.     (goto-char (point-min))
  43.     (skip-chars-forward "\n")
  44.     (narrow-to-region (point) (point-max))
  45.     (setq from (point))
  46.     (let ((fpre (and fill-prefix (not (equal fill-prefix ""))
  47.              (regexp-quote fill-prefix))))
  48.       ;; Delete the fill prefix from every line except the first.
  49.       ;; The first line may not even have a fill prefix.
  50.       (and fpre
  51.        (progn
  52.          (if (>= (length fill-prefix) fill-column)
  53.          (error "fill-prefix too long for specified width"))
  54.          (goto-char (point-min))
  55.          (forward-line 1)
  56.          (while (not (eobp))
  57.            (if (looking-at fpre)
  58.            (delete-region (point) (match-end 0)))
  59.            (forward-line 1))
  60.          (goto-char (point-min))
  61.          (and (looking-at fpre) (forward-char (length fill-prefix)))
  62.          (setq from (point)))))
  63.     ;; from is now before the text to fill,
  64.     ;; but after any fill prefix on the first line.
  65.  
  66.     ;; Make sure sentences ending at end of line get an extra space.
  67.     (goto-char from)
  68.     (while (re-search-forward "[.?!][])""']*$" nil t)
  69.       (insert ? ))
  70.     ;; The change all newlines to spaces.
  71.     (subst-char-in-region from (point-max) ?\n ?\ )
  72.     ;; Flush excess spaces, except in the paragraph indentation.
  73.     (goto-char from)
  74.     (skip-chars-forward " \t")
  75.     (while (re-search-forward "   *" nil t)
  76.       (delete-region
  77.        (+ (match-beginning 0)
  78.       (if (save-excursion
  79.            (skip-chars-backward " ])\"'")
  80.            (memq (preceding-char) '(?. ?? ?!)))
  81.           2 1))
  82.        (match-end 0)))
  83.     (goto-char (point-max))
  84.     (delete-horizontal-space)
  85.     (insert "  ")
  86.     (goto-char (point-min))
  87.     (let ((prefixcol 0))
  88.       (while (not (eobp))
  89.     (move-to-column (1+ fill-column))
  90.     (if (eobp)
  91.         nil
  92.       ;; Move back to start of word.
  93.       (skip-chars-backward "^ \n")
  94.       (if (if (zerop prefixcol) (bolp) (>= prefixcol (current-column)))
  95.           ;; Move back over whitespace before the word.
  96.           (skip-chars-forward "^ \n")
  97.         ;; Normally, move back over the single space between the words.
  98.         (forward-char -1)))
  99.     ;; Replace all whitespace here with one newline.
  100.     ;; Insert before deleting, so we don't forget which side of
  101.     ;; the whitespace point or markers used to be on.
  102.     (skip-chars-backward " ")
  103.     (insert ?\n)
  104.     (delete-horizontal-space)
  105.     ;; Insert the fill prefix at start of each line.
  106.     ;; Set prefixcol so whitespace in the prefix won't get lost.
  107.     (and (not (eobp)) fill-prefix (not (equal fill-prefix ""))
  108.          (progn
  109.            (insert fill-prefix)
  110.            (setq prefixcol (current-column))))
  111.     ;; Justify the line just ended, if desired.
  112.     (and justify-flag (not (eobp))
  113.          (progn
  114.            (forward-line -1)
  115.            (justify-current-line)
  116.            (forward-line 1)))))))
  117.  
  118. (defun fill-paragraph (arg)
  119.   "Fill paragraph at or after point.
  120. Prefix arg means justify as well."
  121.   (interactive "P")
  122.   (save-excursion
  123.     (forward-paragraph)
  124.     (or (bolp) (newline 1))
  125.     (let ((end (point)))
  126.       (backward-paragraph)
  127.       (fill-region-as-paragraph (point) end arg))))
  128.  
  129. (defun fill-region (from to &optional justify-flag)
  130.   "Fill each of the paragraphs in the region.
  131. Prefix arg (non-nil third arg, if called from program)
  132. means justify as well."
  133.   (interactive "r\nP")
  134.   (save-restriction
  135.    (narrow-to-region from to)
  136.    (goto-char (point-min))
  137.    (while (not (eobp))
  138.      (let ((initial (point))
  139.        (end (progn
  140.          (forward-paragraph 1) (point))))
  141.        (forward-paragraph -1)
  142.        (if (>= (point) initial)
  143.        (fill-region-as-paragraph (point) end justify-flag)
  144.      (goto-char end))))))
  145.  
  146. (defun justify-current-line ()
  147.   "Add spaces to line point is in, so it ends at fill-column."
  148.   (interactive)
  149.   (save-excursion
  150.    (save-restriction
  151.     (let (ncols nwhites beg indent flags)
  152.       (beginning-of-line)
  153.       (forward-char (length fill-prefix))
  154.       (skip-chars-forward " \t")
  155.       (setq indent (current-column))
  156.       (setq beg (point))
  157.       (end-of-line)
  158.       (narrow-to-region beg (point))
  159.       (goto-char beg)
  160.       (while (re-search-forward "   *" nil t)
  161.     (delete-region
  162.      (+ (match-beginning 0)
  163.         (if (save-excursion
  164.          (skip-chars-backward " ])\"'")
  165.          (memq (preceding-char) '(?. ?? ?!)))
  166.         2 1))
  167.      (match-end 0)))
  168.       (goto-char beg)
  169.       (while (re-search-forward "[.?!][])""']*\n" nil t)
  170.     (forward-char -1)
  171.     (insert ? ))
  172.       (goto-char (point-max))
  173.       ;; Note that the buffer bounds start after the indentation,
  174.       ;; so the columns counted by INDENT don't appear in (current-column).
  175.       (setq ncols (- fill-column (current-column) indent))
  176.       ;; Count word-boundaries in the line.
  177.       (setq nwhites 0)
  178.       (while (search-backward " " nil t)
  179.     (skip-chars-backward " ")
  180.     (setq nwhites (1+ nwhites)))
  181.       (if (> nwhites 0)
  182.       (progn
  183.         ;; Add space uniformly as far as we can.
  184.         (goto-char (point-max))
  185.         (while (search-backward " " nil t)
  186.           (insert-char ?\  (/ ncols nwhites))
  187.           (skip-chars-backward " "))
  188.         ;; Make a bit vector for where to add the rest.
  189.         (setq ncols (% ncols nwhites))
  190.         (setq flags (make-string nwhites 0))
  191.         ;; Randomly set NCOLS different bits.
  192.         (while (> ncols 0)
  193.           (let ((where (% (logand 262143 (random)) nwhites)))
  194.         (or (> (aref flags where) 0)
  195.             (progn
  196.               (aset flags where 1)
  197.               (setq ncols (1- ncols))))))
  198.         ;; Insert a space at the boundaries flagged in the vector.
  199.         (goto-char (point-max))
  200.         (let ((where 0))
  201.           (while (search-backward " " nil t)
  202.         (if (> (aref flags where) 0)
  203.             (insert " "))
  204.         (setq where (1+ where))
  205.         (skip-chars-backward " ")))))))))
  206.  
  207. (defun fill-individual-paragraphs (min max &optional justifyp mailp)
  208.   "Fill each paragraph in region according to its individual fill prefix.
  209. Calling from a program, pass range to fill as first two arguments.
  210. Optional third and fourth arguments JUSTIFY-FLAG and MAIL-FLAG:
  211. JUSTIFY-FLAG to justify paragraphs (prefix arg),
  212. MAIL-FLAG for a mail message, i. e. don't fill header lines."
  213.   (interactive "r\nP")
  214.   (save-restriction
  215.     (save-excursion
  216.       (goto-char min)
  217.       (beginning-of-line)
  218.       (if mailp 
  219.       (while (looking-at "[^ \t\n]*:")
  220.         (forward-line 1)))
  221.       (narrow-to-region (point) max)
  222.       ;; Loop over paragraphs.
  223.       (while (progn (skip-chars-forward " \t\n") (not (eobp)))
  224.     (beginning-of-line)
  225.     (let ((start (point))
  226.           fill-prefix fill-prefix-regexp)
  227.       ;; Find end of paragraph, and compute the smallest fill-prefix
  228.       ;; that fits all the lines in this paragraph.
  229.       (while (progn
  230.            ;; Update the fill-prefix on the first line
  231.            ;; and whenever the prefix good so far is too long.
  232.            (if (not (and fill-prefix
  233.                  (looking-at fill-prefix-regexp)))
  234.                (setq fill-prefix
  235.                  (buffer-substring (point)
  236.                            (save-excursion (skip-chars-forward " \t") (point)))
  237.                  fill-prefix-regexp
  238.                  (regexp-quote fill-prefix)))
  239.            (forward-line 1)
  240.            ;; Now stop the loop if end of paragraph.
  241.            (and (not (eobp))
  242.             (not (looking-at paragraph-separate))
  243.             (save-excursion
  244.               (not (and (looking-at fill-prefix-regexp)
  245.                     (progn (forward-char (length fill-prefix))
  246.                        (looking-at paragraph-separate))))))))
  247.       ;; Fill this paragraph, but don't add a newline at the end.
  248.       (let ((had-newline (bolp)))
  249.         (fill-region-as-paragraph start (point) justifyp)
  250.         (or had-newline (delete-char -1))))))))
  251.  
  252.